// pages/clubinfoedit/clubinfoedit.js

const {
  $Message
} = require('../../iview/base/index');

var app=getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    typearr: ["艺术类", "科学类", "实践类", "其他类"],
    index: 0,
    pictures: [],
    clubinfo:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      clubid: options.clubid
    })
    wx.setNavigationBarTitle({
      title: '修改社团信息'
    })
    const db = wx.cloud.database()
    const clubco = db.collection("club")
    var that = this
    console.log(options.clubid)
    clubco
    .doc(options.clubid)
    .get()
    .then(function(res) {
      that.setData({
        clubinfo: res.data,
        name: res.data.name,
        profile: res.data.profile,
        contact: res.data.contact,
        slogan: res.data.slogan
      })
      // if (res.data[0].pictures != undefined) {
      //   wx.cloud.callFunction({
      //     name: 'deleteFile',
      //     data: {
      //       file: res.data[0].pictures
      //     }
      //   })
      // }
    })
   

  },

  onChangeName: function (e) {
    var _this = this
    _this.setData({
      name: e.detail
    })
  },

  onChangeProfile: function (e) {
    var _this = this
    _this.setData({
      profile: e.detail
    })
  },

  onChangeContact: function (e) {
    var _this = this
    _this.setData({
      contact: e.detail
    })
  },

  onChangeSlogan: function (e) {
    var _this = this
    _this.setData({
      slogan: e.detail
    })
  },

  subscribe:function(e){

  },

  radioChange: function(e) {
    this.setData({
      type: e.detail.value
    })
  },

  inputChange(e) {
    var that = this
    switch (e.currentTarget.id) {
      case "name":
        {
          that.setData({
            name: e.detail.value
          })
        }
        break
      case "slogan":
        {
          that.setData({
            slogan: e.detail.value
          })
        }
        break
      case "profile":
        {
          that.setData({
            profile: e.detail.value
          })
        }
        break
      case "contact":
        {
          that.setData({
            contact: e.detail.value
          })
        }
        break
    }
  },

  commit: function(e) {
    var that = this
    console.log(this.data)
    if (this.data.name != undefined && this.data.slogan != undefined && this.data.profile != undefined && this.data.contact != undefined && this.data.type != undefined) {
      var _club = {
        id: that.data.clubid,
        name: that.data.name,
        slogan: that.data.slogan,
        profile: that.data.profile,
        contact: that.data.contact,
        // type: that.data.type,
        pictures: that.data.pictures,
        avatar:that.data.avatar
      }
      wx.cloud.callFunction({
        name: "editClubInfo",
        data: {
          club: _club
        }
      }).then(function(e) {
        $Message({
          content: '社团信息更新成功',
          type: 'success'
        })
        setTimeout(function(){
          wx.navigateBack({
            delta: 1,
          })
        },1500)
      })
    } else {
      $Message({
        content: '请完整填写全部信息哦~',
        type: 'warning'
      })
    }
  },

  uploadPic: function(e) {
    var that = this
    if (that.data.pictures == undefined || that.data.pictures.length < 3) {
      wx.chooseImage({
        success: function(img) {
          var index = 1
          if (that.data.pictures != undefined)
            index = that.data.pictures.length + 1;
          wx.cloud.uploadFile({
            cloudPath: "clubimage/" + that.data.clubinfo._id + index.toString() + ".png",
            filePath: img.tempFilePaths[0]
          }).then(function(upldres) {
            console.log(upldres)
            //记录下来照片的ID
            var temp = that.data.pictures
            if (temp == undefined) {
              temp = [upldres.fileID]
            } else {
              temp.push(upldres.fileID)
            }
            that.setData({
              pictures: temp
            })
          })
        },
      })
    } 
    else {
      $Message({
        content: '最多上传3张图片哦',
        type: 'warning'
      })
    }
  },

  uploadAva:function(e){
    
    var that=this
    if(that.data.avatar==undefined){
      wx.chooseImage({
        success:function(img){
          wx.cloud.uploadFile({
            cloudPath: "clubimage/" + that.data.clubinfo._id + "_avatar" + ".png",
            filePath: img.tempFilePaths[0]
          }).then(function (upldres) {
            console.log(upldres)
            //记录下来照片的ID
            that.setData({
              avatar: upldres.fileID
            })
          })
        }
      })
    }
    else{
      $Message({
        content: '您已经上传过图片了哦',
        type: 'warning'
      })
    }
  },

  // onChange(event) {
  //   const { picker, value, index } = event.detail;
  //   Toast(`当前值：${value}, 当前索引：${index}`);
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})